package com.meida.module.bus.provider.handler;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.alibaba.fastjson.util.TypeUtils;
import com.meida.common.base.module.ExportField;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.ExportInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusReportOuttwo;
import com.meida.module.bus.provider.service.BusReportOutthreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component("busReportOutThreeExportHandler")
public class BusReportOutThreeExportHandler implements ExportInterceptor<BusReportOuttwo> {

    @Autowired
    BusReportOutthreeService busReportOutthreeService;


    @Override
    public void initExcelExportEntity(ExportField exportField, List<ExcelExportEntity> entityList) {
        String key = (String) exportField.getKey();
        String name = exportField.getName();
        if (FlymeUtils.isEmpty(key)) {
            List<ExportField> children = exportField.getChildren();
            List<ExcelExportEntity> excelExportEntityList = new ArrayList<>();
            for (ExportField childrenExportField : children) {
                childrenExportField.setGroupName(name);
                if ("金额(元)".equals(name)) {
                    exportField.setKey("a");
                }
                if ("累计申请".equals(name)) {
                    exportField.setKey("b");
                }
                if ("本次申请".equals(name)) {
                    exportField.setKey("c");
                }
                if ("管理公司审核".equals(name)) {
                    exportField.setKey("d");
                }
                excelExportEntityList.add(new ExcelExportEntity(childrenExportField.getName(), childrenExportField.getKey()));
            }
            exportField.setList(excelExportEntityList);
            exportField.setChildren(new ArrayList<>());
        }
    }

    @Override
    public ResultBody initData(Map params) {
        List<EntityMap> busReportOutthreeList = busReportOutthreeService.getExportData(params);
        ResultBody resultBody = new ResultBody();
        List<EntityMap> list = new ArrayList();
        if (FlymeUtils.isEmpty(busReportOutthreeList)) {
            list.add(new EntityMap());
            resultBody.data(list);
        } else {
            resultBody.data(busReportOutthreeList);
        }
        for (EntityMap entityMap : busReportOutthreeList) {
            List<EntityMap> list1 = new ArrayList<>();
            EntityMap entityMap1 = new EntityMap();
            entityMap1.put("syntheticalPrice", entityMap.getBigDecimal("syntheticalPrice"));
            entityMap1.put("sumPrice", entityMap.getBigDecimal("sumPrice"));
            list1.add(entityMap1);
            entityMap.put("a", list1);

            List<EntityMap> list2 = new ArrayList<>();
            EntityMap entityMap2 = new EntityMap();
            entityMap2.put("sumApplyQuantities", entityMap.getBigDecimal("sumApplyQuantities"));
            entityMap2.put("sumApplyAmount", entityMap.getBigDecimal("sumApplyAmount"));
            list2.add(entityMap2);
            entityMap.put("b", list2);

            List<EntityMap> list3 = new ArrayList<>();
            EntityMap entityMap3 = new EntityMap();
            entityMap3.put("theApplyQuantities", entityMap.getBigDecimal("theApplyQuantities"));
            entityMap3.put("theApplyAmount", entityMap.getBigDecimal("theApplyAmount"));
            list3.add(entityMap3);
            entityMap.put("c", list3);

            List<EntityMap> list4 = new ArrayList<>();
            EntityMap entityMap4 = new EntityMap();
            entityMap4.put("checkQuantities", entityMap.getBigDecimal("checkQuantities"));
            entityMap4.put("checkAmount", entityMap.getBigDecimal("checkAmount"));
            list4.add(entityMap4);
            entityMap.put("d", list4);
        }
        return resultBody;
    }
}

